package com.apes.framework.plugin.log.timer;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 功能：
 *
 * @author xul
 * @create 2018-04-20 15:55
 */
public abstract class LogTimer {
    protected static final Logger logger = LogManager.getLogger(LogTimer.class);

    public <T> T exec(String describe) {
        long beginTime = System.nanoTime(); /**单位：微秒**/
        T result = exec();
        long endTime = System.nanoTime();
        debug(describe, beginTime, endTime);
        return result;
    }

    public static void debug(String describe, long beginTime, long endTime) {
        if (logger.isDebugEnabled()) {
            long total = endTime - beginTime;
            long ms = total / 1000 / 1000; /**毫秒**/
            total -= ms * 1000 * 1000;
            long us = total / 1000; /**微秒**/
            total -= us * 1000;
            long ns = total; /**纳秒**/
            logger.debug(String.format("%s耗时：%s ms, %s us, %s ns", describe, ms, us, ns));
        }
    }

    public abstract <T> T exec();

}
